
use prisma_client_rust::WhereQuery;
use rspc::{Type};
use serde::{Serialize};

use crate::{opinion::{self, OpinionManager, OpinionParams, SaveProgrammeBookData}, prisma};

use super::{utils::LibraryRequest, RouterBuilder};

pub(crate) fn mount() -> RouterBuilder {
    return RouterBuilder::new()
        .query("version", |t| {
            #[derive(Serialize, Type)]
			pub struct BuildInfo {
				version: &'static str,
			}

			t(|_, _: ()| BuildInfo {
				version: "0.0.1",
			})
        })
        .library_query("get", |t| {
            t(|_, args: OpinionParams, library| async move {
                let mut query = library.db
                    .opinion()
                    .find_many(vec![
                        // prisma::opinion::description::contains(args.description.unwrap().to_string()),
                        // prisma::opinion::content::contains(args.content.unwrap().to_string())
                    ]);

                if !args.slug.is_none() {
                    query
                        .add_where(prisma::opinion::slug::equals(args.slug.unwrap().to_string()));
                }

                if !args.description.is_none() {
                    query
                        .add_where(prisma::opinion::description::contains(args.description.clone().unwrap().to_string()));
                }

                if !args.content.is_none() {
                    query
                        .add_where(prisma::opinion::content::contains(args.content.clone().unwrap().to_string()));
                }

                // let skip = (args.page - 1) * 20;

                let results = query
                    // .skip(skip.into())
                    // .take(20)
                    .include(prisma::opinion::include!({ programme_book }))
                    .exec()
                    .await?;

                // if !args.state.is_none() {
                //     results = results.clone().into_iter().filter(|item| {
                //         return item.programme_book.clone().unwrap().state == Some(args.state.clone().unwrap().to_string());
                //     }).collect();
                // }

                // if !args.book_state.is_none() {
                //     results = results.clone().into_iter().filter(|item| {
                //         return item.programme_book.clone().unwrap().book_state == Some(args.book_state.clone().unwrap().to_string());
                //     }).collect();
                // }

                // let results = OpinionManager::get(library.db, args).await?;
                Ok(results)
			})
        })
        .library_query("slugs", |t| {

            t(|_, _: (), library| async move {
                Ok(opinion::OpinionManager::slugs(library.db).await?)
            })
        })
        .library_query("create", |t| {
            t(|_, _: (), library| async move { Ok(OpinionManager::create(library.db).await?) })
        })
        .query("update", |t| t(|db, _:()| async move {
            Ok(OpinionManager::update(db.client).await?)
        }))
        .library_mutation("save_programme_book", |t| {
            t(|_, args: SaveProgrammeBookData, library| async move {
                Ok(opinion::OpinionManager::save_programme_book(library.db, args).await?)
            })
        });
}
